﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Data.SqlClient;

namespace tp
{
    public partial class guanliyuan : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["guanliyuan"] != null)
            {
                this.Userguanli.Text= (string)Session["guanliyuan"];
            }

        }
        protected void ibsee_Click(object sender, ImageClickEventArgs e)
        {
            string strCn = "Data Source=pc.hstc.cn;Initial Catalog=dhd2014112142;User ID=2014112142;Password=19961124";
            SqlConnection cn = new SqlConnection(strCn);
            try
            {
                cn.Open();
                string strSql = "SELECT  No,Name,Age,College,Birth,Iden,Addr  FROM Infomation  ";
                SqlCommand cmd = new SqlCommand(strSql, cn);
                SqlDataReader dr = cmd.ExecuteReader();//ExecuteReader是一个对象，需要重新定义对象
                this.GridView1.DataSource = dr;
                this.GridView1.DataBind();
                dr.Close();
            }
            catch (Exception ex)
            {
                this.lbl.Text = ex.Message;
            }
            finally
            {
                cn.Close();
            }
        }
        protected void Page_PreInit(object sender, EventArgs e)
        {
            if (Request.QueryString["Theme"] != null)
                Page.Theme = Request.QueryString["Theme"].ToString();

        }
        protected void ibhuanfu_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("guanliyuan.aspx?Theme=" + ddlhuanfu.SelectedValue + "");
        }

        protected void ddlhuanfu_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
        }
        //删除
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string sqlstr = "delete from Infomation where id='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
            string strCn = "Data Source=pc.hstc.cn;Initial Catalog=dhd2014112142;User ID=2014112142;Password=19961124";
            SqlConnection cn = new SqlConnection(strCn);
            SqlCommand cmd = new SqlCommand(sqlstr, cn);
            cn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            dr.Close();
        }
        //更新
        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            string strCn = "Data Source=pc.hstc.cn;Initial Catalog=dhd2014112142;User ID=2014112142;Password=19961124";
            SqlConnection cn = new SqlConnection(strCn);
            string sqlstr = "update Infomation set 字段1='"
                + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',字段2='"
                + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() + "',字段3='"
                + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "' where id='"
                + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
            SqlCommand cmd = new SqlCommand(sqlstr, cn);
            cn.Open();
            cmd.ExecuteNonQuery();
            cn.Close();
            GridView1.EditIndex = -1;
        }
       
    }
}